package com.plm.frame.action;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.plm.frame.core.Constants;
import com.plm.frame.model.Admin;
import com.plm.frame.model.Dingdan;
import com.plm.frame.model.Fenlei;
import com.plm.frame.model.Product;
import com.plm.frame.model.User;
import com.plm.frame.service.AdminService;
import com.plm.frame.service.DingdanService;
import com.plm.frame.service.FenleiService;
import com.plm.frame.service.ProductService;
import com.plm.frame.service.UserService;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class AdminAction extends BaseAction{

	@Autowired
	private AdminService adminService;

	@Resource
	private FenleiService fenleiService;
	
	@Resource
	private ProductService productService;
	
	@Resource
	private DingdanService dingdanService;
	
	@Resource
	private UserService userService;
	
	@RequestMapping("/adminindex")
	public String getIndexPage(Map<String,List> m){
		m.put("fl", fenleiService.selectFenleiAll());
		return "adminindex";
	}
	
	@RequestMapping("/adminfenlei")
	public String adminfenlei(Map<String,Object> m){
		m.put("fl", fenleiService.selectFenlei(0));
		List<Fenlei> fenleis = fenleiService.selectFenleiAll();
		Integer pageSize = fenleis.size()%5==0?fenleis.size()/5:fenleis.size()/5+1;
		m.put("pageSize",pageSize);
		m.put("pageNumber",1);
		return "adminfenlei";
	}

	@RequestMapping("/deletefenlei")
	public String deletefenlei(Map<String,Object> m,Fenlei fenlei){
		fenleiService.deleteFenlei(fenlei);
		m.put("fl",fenleiService.selectFenlei(0));
		List<Fenlei> fenleis = fenleiService.selectFenleiAll();
		Integer pageSize = fenleis.size()%5==0?fenleis.size()/5:fenleis.size()/5+1;
		m.put("pageSize",pageSize);
		m.put("pageNumber",1);
		return "adminfenlei";
	}

	@RequestMapping("/toaddfenlei")
	public String toaddfenlei(){

		return "adminaddfenlei";
	}

	@RequestMapping("/addfenlei")
	public String addfenlei(Map<String,Object> m,Fenlei fenlei){
		List<Fenlei> fenleis = fenleiService.selectFenleiAll();
		for (Fenlei f:fenleis) {
			if (fenlei.getName().equals(f.getName())){
				m.put("error","该分类已经存在");
				return "adminfenlei";
			}
		}
		fenleiService.addFenlei(fenlei);
		m.put("fl",fenleiService.selectFenlei(0));
		fenleis = fenleiService.selectFenleiAll();
		Integer pageSize = fenleis.size()%5==0?fenleis.size()/5:fenleis.size()/5+1;
		m.put("pageSize",pageSize);
		m.put("pageNumber",1);
		return "adminfenlei";
	}

	@RequestMapping("/admindingdan")
	public String admindingdan(Map<String,List> m,Dingdan dingdan){
		m.put("dd", dingdanService.selectDingdan(dingdan));
		return "admindingdan";
	}
	
	@RequestMapping("/adminuser")
	public String adminUser(Map<String,List> m,User user){
		m.put("us", userService.selectUser(user));
		return "adminuser";
	}
	

	
	@RequestMapping("/adminlogin")
	public String login(Map<String,List> m){
		return "adminlogin";
	}
	
	@RequestMapping("/adminVerification")
	public String adminVerification(Map<String,List> m,Admin admin,HttpSession session){
		Admin dbUser=adminService.checkUser(admin);
		if(dbUser!=null){		
			session.setAttribute(Constants.LOGIN_USER, dbUser);
			System.out.println("userlogin:成功登陆");
			return "redirect:adminindex";
		}
		else{
			return "failed";
		}
	}

	@RequestMapping("/adminlogout")
	public String logout(Map<String,List> m,HttpSession session) {
		getSession().invalidate();
		return "adminlogin";
	}
	
	@RequestMapping("/adminproduct")
	public String adminproduct(Map<String,List> m,HttpSession session,Product product) {
		m.put("pd",productService.selectProduct(product));
		return "adminproduct";
	}

	@RequestMapping("/turnPage")
	public String turnPage(HttpSession session, HttpServletRequest request) {
        String pageNumber = request.getParameter("pageNumber");
        List<Fenlei> fenleis = fenleiService.selectFenleiAll();
        Integer pageSize = fenleis.size()%5==0?fenleis.size()/5:fenleis.size()/5+1;
        if (Integer.parseInt(pageNumber)>=pageSize){
            List<Fenlei> fenleis1 = fenleiService.selectFenlei((pageSize - 1) * 5);
            session.setAttribute("fl",fenleis1);
            session.setAttribute("pageSize",pageSize);
            session.setAttribute("pageNumber",pageSize);
            return "adminfenlei";
        }
        List<Fenlei> fenleis1 = fenleiService.selectFenlei((Integer.parseInt(pageNumber)-1)* 5);
        session.setAttribute("fl",fenleis1);
		session.setAttribute("pageSize",pageSize);
		session.setAttribute("pageNumber",pageNumber);
		return "adminfenlei";
	}

	@RequestMapping("/adminusers")
	public String adminusers(Map<String,List> m,User user){
		m.put("usersss", userService.selectUser(user));
		return "adminuser";
	}
	@RequestMapping("/admindeleteUser")
	public String admindeleteUser(Map<String,List> m,User user){
		userService.deleteUser(user);
		return "redirect:adminusers";
	}

}
